package com.jrx.anytxn.transaction.job.dailyAccount.config;

import com.jrx.anytxn.transaction.config.AnytxnBatchProperties;
import com.jrx.anytxn.transaction.job.dailyAccount.bean.DailyAccountBean;
import com.jrx.anytxn.transaction.enums.DailyAccountEnum;
import com.jrx.anytxn.transaction.job.dailyAccount.step.DailyAccountProcessor;
import com.jrx.anytxn.transaction.job.dailyAccount.step.DailyAccountReader;
import com.jrx.anytxn.transaction.job.dailyAccount.step.DailyAccountWriter;
import com.jrx.anytxn.transaction.pojo.DailyAccount;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.ItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resource;

/**
 * 日终T24核心汇总记账
 *
 * @Author yhy
 * @Date 2023/4/29 14:31
 */
@Configuration
@EnableConfigurationProperties(AnytxnBatchProperties.class)
public class DailyAccountConfiguration {


    @Resource(name = "bizSqlSessionFactory")
    SqlSessionFactory sqlSessionFactory;

    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    @Autowired
    private AnytxnBatchProperties anytxnBatchProperties;

    @Autowired
    private PlatformTransactionManager platformTransactionManager;

    @Autowired
    private DailyAccountProcessor dailyAccountProcessor;

    @Autowired
    private DailyAccountWriter dailyAccountWriter;

    @Bean
    public Job dailyAccountJob() {
        return jobBuilderFactory.get("dailyAccountJob")
                // 日终T24核心汇总记账：BEPS-小额;HVPS-大额处理
                .start(dailyAccountBepsHvpsStep())
                // 日终T24核心汇总记账：IBPS-网银清算处理
                .next(dailyAccountIbpsStep())
                .build();
    }

    @Bean
    public Step dailyAccountBepsHvpsStep() {
        return stepBuilderFactory.get("dailyAccountBepsHvpsStep")
                .<DailyAccount, DailyAccountBean>chunk(anytxnBatchProperties.getDailyAccountConfiguration().getChunkLimit())
                //表示一批处理5000条记录，数据是逐条发送给processor处理的，处理5000条记录后，统一返回给writer进行保存处理
                .reader(dailyAccountBepsHvpsReader())
                .processor(dailyAccountProcessor)
                .writer(dailyAccountWriter)
                .transactionManager(platformTransactionManager)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anytxnBatchProperties.getDailyAccountConfiguration().getThrottleLimit())//多线程*/
                .build();
    }

    @Bean
    @StepScope
    public ItemReader<DailyAccount> dailyAccountBepsHvpsReader() {
        return new DailyAccountReader(DailyAccountEnum.T24_CHANNEL_BEPS_HVPS.getCode(), sqlSessionFactory);
    }

    @Bean
    public Step dailyAccountIbpsStep() {
        return stepBuilderFactory.get("dailyAccountIbpsStep")
                .<DailyAccount, DailyAccountBean>chunk(anytxnBatchProperties.getDailyAccountConfiguration().getChunkLimit())
                //表示一批处理5000条记录，数据是逐条发送给processor处理的，处理5000条记录后，统一返回给writer进行保存处理
                .reader(dailyAccountIbpsReader())
                .processor(dailyAccountProcessor)
                .writer(dailyAccountWriter)
                .transactionManager(platformTransactionManager)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anytxnBatchProperties.getDailyAccountConfiguration().getThrottleLimit())//多线程*/
                .build();
    }

    @Bean
    @StepScope
    public ItemReader<DailyAccount> dailyAccountIbpsReader() {
        return new DailyAccountReader(DailyAccountEnum.T24_CHANNEL_IBPS.getCode(), sqlSessionFactory);
    }
}
